/*
 * @(#)FileTreeSelectionListener.java   09/09/20
 * 
 * Copyright (C) 2009 Ecole des Mines d'Ales
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Contact: yulinz88@gmail.com
 */



/**
 *
 */
package fr.ema.arch3d.gea.mainGUI.listeners;

import fr.ema.arch3d.gea.mainGUI.fileDirectory.FileNode;
import fr.ema.arch3d.gea.mainGUI.fileDirectory.FileTree;

import java.io.IOException;

import java.net.MalformedURLException;

import javax.swing.JEditorPane;
import javax.swing.JTabbedPane;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.TreePath;

//~--- classes ----------------------------------------------------------------

/**
 * Class description
 *
 *
 * @version        1.0, 09/09/13
 * @author         Huaxi (Yulin) ZHANG
 */
public class FileTreeSelectionListener implements TreeSelectionListener {

    /** Field description */
    JTabbedPane editorTabbedPane;

    /** Field description */
    FileTree fileTree;

    /** Field description */
    JEditorPane xmlEditorPane;

    /**
     * Constructs...
     *
     * @param fileTree
     * @param editorTabbedPane
     */
    public FileTreeSelectionListener(FileTree fileTree, JTabbedPane editorTabbedPane) {
        super();
        this.fileTree         = fileTree;
        this.editorTabbedPane = editorTabbedPane;
        xmlEditorPane=(JEditorPane) editorTabbedPane.getComponent(1);
    }

    /**
     * Method description
     *
     * @param arg0
     */
    public void valueChanged(TreeSelectionEvent arg0) {
	editorTabbedPane.setSelectedIndex(1);
        // find select node in the tree
        TreePath path = fileTree.getSelectionPath();

        if (path == null) {
            return;
        }

        FileNode selectedNode = (FileNode) path.getLastPathComponent();

        // active the correspondent graph cell to selection
        try {
            if (selectedNode.getFile().toURL().toString().endsWith(".xml")) {
                java.net.URL fileURL = null;

                fileURL = selectedNode.getFile().toURL();
                System.out.println(fileURL);

                if (fileURL != null) {
                    try {
                        xmlEditorPane.setPage(fileURL);
                    } catch (IOException e) {
                        System.err.println("Attempted to read a bad URL: " + fileURL);
                    }
                } else {
                    System.err.println("Couldn't find file: TextSamplerDemoHelp.html");
                }

                
            }
        } catch (MalformedURLException e) {

            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
